package robotleMvc.frameworks.lizhi.fensan
{
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.FensanBiandaShader;
	import _Pan3D.program.shaders.FensanShader;
	import _Pan3D.program.shaders.KuosanAndBiandaShader;
	import _Pan3D.program.shaders.TestFensanBiandaShader;
	import _Pan3D.program.shaders.UvMoveNewShader;
	import _Pan3D.program.shaders.ZhuanQuanShader;
	
	import flash.display3D.Context3D;
	import flash.display3D.Program3D;
	import flash.utils.setTimeout;
	
	import org.robotlegs.mvcs.Mediator;
	
	import robotleMvc.ui.lizhi.fensan.FensanBiandaDisplaySprite;
	import robotleMvc.ui.lizhi.fensan.FensanDisplaySprite;
	import robotleMvc.ui.lizhi.fensan.FensanModel;
	import robotleMvc.ui.lizhi.fensan.FensanView;
	import robotleMvc.ui.lizhi.fensan.TestFensanDisplaySprite;
	import robotleMvc.ui.lizhi.kuosAndBianda.KuosanAndBiandaDisplaySprite;
	import robotleMvc.ui.lizhi.uvMove.UvMoveDisplaySprite;
	import robotleMvc.ui.lizhi.zhuanQuan.ZhuanQuanDispay3DSprite;
	
	public class FensanMediator extends Mediator
	{
		[Inject]
		public var fensanView:FensanView;
		[Inject]
		public var fensanModel:FensanModel;
		
		private var _testFensanBiandaSprite:TestFensanDisplaySprite;
		private var _uvMoveDisplaySprite:UvMoveDisplaySprite;
		private var _zhuanQuanDispay3DSprite:ZhuanQuanDispay3DSprite;
		private var _kuosanAndbiandaSprite:KuosanAndBiandaDisplaySprite;
        public function FensanMediator()
		{
			super();
			
		}
		override public function onRegister():void
		{
			fensanModel.display3DContainer=fensanView.display3DContainer;
			fensanModel.context3D=fensanView.context3D;
	        maskFensans();
			addEvents();
		}
		
		private function addEvents():void
		{
			this.addContextListener( FensanEvent.SHOW_FENSAN_XIAOGUO,showFensanXiaoGuo);
		}
		private function showFensanXiaoGuo(evt:FensanEvent):void
		{
		
			_uvMoveDisplaySprite.play=true;
			var intervalId:uint = setTimeout(myRepeatingFunction, 1000*30/60);
			function myRepeatingFunction():void{
				_zhuanQuanDispay3DSprite.play=true;
				_kuosanAndbiandaSprite.clearOldBuff();
				_kuosanAndbiandaSprite.play=true;
				return;
				_testFensanBiandaSprite.clearOldBuff();
				_testFensanBiandaSprite.play=true;
				
			}
		}
		private function maskFensans():void
		{
			addFensanBianda();
			addUvMoves();
			addZhuanQuans();
			addKuosanAndbiandas();
			_uvMoveDisplaySprite.play=false;
			_testFensanBiandaSprite.play=false;
			_zhuanQuanDispay3DSprite.play=false;
		}
		private function addZhuanQuans():void
		{
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			_zhuanQuanDispay3DSprite =new ZhuanQuanDispay3DSprite(context3D);
			_zhuanQuanDispay3DSprite.url ="jiantou01";
			_zhuanQuanDispay3DSprite.x=500
			_zhuanQuanDispay3DSprite.y=50;
			_zhuanQuanDispay3DSprite.z=900
			_zhuanQuanDispay3DSprite.scale=0.5;
			display3DContainer.addChild(_zhuanQuanDispay3DSprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram( ZhuanQuanShader.ZHUANQUAN_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			_zhuanQuanDispay3DSprite.program3D=treeShaderProgram3d;
		}
		
		private function addUvMoves():void
		{
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			 _uvMoveDisplaySprite =new UvMoveDisplaySprite(context3D);
			 _uvMoveDisplaySprite.url ="uvbomb";
			 _uvMoveDisplaySprite.x=500
			 _uvMoveDisplaySprite.y=50;
			 _uvMoveDisplaySprite.z=900;
			 _uvMoveDisplaySprite.scale=0.5
			display3DContainer.addChild(_uvMoveDisplaySprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(  UvMoveNewShader.UVMOVE_NEW_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			_uvMoveDisplaySprite.program3D=treeShaderProgram3d;
			
			
		}
		
		private function addFensanBianda():void
		{
			// TODO Auto Generated method stub
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			_testFensanBiandaSprite =new TestFensanDisplaySprite(context3D);
			_testFensanBiandaSprite.url ="testfensanbianda";
			_testFensanBiandaSprite.x=500
			_testFensanBiandaSprite.y=50;
			_testFensanBiandaSprite.z=900
			_testFensanBiandaSprite.scale=1;
			display3DContainer.addChild(_testFensanBiandaSprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram( TestFensanBiandaShader.TEST_FENSAN_BIANDA_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			_testFensanBiandaSprite.program3D=treeShaderProgram3d;
		}
		private function addKuosanAndbiandas():void
		{
	
			// TODO Auto Generated method stub
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			_kuosanAndbiandaSprite =new KuosanAndBiandaDisplaySprite(context3D);
			_kuosanAndbiandaSprite.url ="testfensanbianda";
			_kuosanAndbiandaSprite.x=500
			_kuosanAndbiandaSprite.y=50;
			_kuosanAndbiandaSprite.z=900
			_kuosanAndbiandaSprite.scale=0.5;
			display3DContainer.addChild(_kuosanAndbiandaSprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(KuosanAndBiandaShader.KUOSAN_AND_BIANDA_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			_kuosanAndbiandaSprite.program3D=treeShaderProgram3d;
	
		}
		
		private function addFensan0():void
		{
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			var display3DSprite:FensanDisplaySprite =new FensanDisplaySprite(context3D);
			display3DSprite.url ="fensanBase";
			display3DSprite.x=0
			display3DSprite.y=0;
			display3DSprite.z=0
			display3DSprite.scale=1;
			display3DContainer.addChild(display3DSprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(  FensanShader.FENSAN_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			display3DSprite.program3D=treeShaderProgram3d;
		}
		private function addFensan1():void
		{
			var context3D:Context3D=fensanModel.context3D;
			var display3DContainer:Display3DContainer=fensanModel.display3DContainer;
			var display3DSprite:FensanBiandaDisplaySprite =new FensanBiandaDisplaySprite(context3D);
			display3DSprite.url ="fensanBase";
			display3DSprite.x=Math.random()*1000-500;
			display3DSprite.y=100
			display3DSprite.z=Math.random()*1000-500;
			display3DSprite.scale=1;
			display3DContainer.addChild(display3DSprite);
			var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(  FensanBiandaShader.FENSAN_BIANDA_SHADER);
			display3DContainer.setProgram3D(treeShaderProgram3d);
			display3DSprite.program3D=treeShaderProgram3d;
		}
	}
}